var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
import React, { useCallback, useContext, useEffect, useState } from "react";
import { getCache, setCache } from "../utils/cache";
var defalut = { isLogin: false };
export var LOGIN_INFO = "LOGIN_INFO";
/**
 * 获取登录信息
 */
export var getLoginInfo = function () {
    return (getCache(LOGIN_INFO) || defalut);
};
var LoginContext = React.createContext({});
export var useLogin = function () { return useContext(LoginContext); };
var LoginProvider = function (props) {
    var _a = __read(useState(defalut), 2), loginState = _a[0], setLoginState = _a[1];
    //监听数据变化
    useEffect(function () {
        if (props.loginState) {
            setLoginState(props.loginState);
            setCache(LOGIN_INFO, props.loginState);
        }
    }, [props.loginState]);
    var login = useCallback(function (res) {
        setLoginState(__assign({}, res));
        setCache(LOGIN_INFO, __assign({}, res));
    }, []);
    var logout = useCallback(function () {
        setLoginState(defalut);
        setCache(LOGIN_INFO, defalut);
    }, []);
    return (React.createElement(LoginContext.Provider, { value: {
            state: loginState,
            login: login,
            logout: logout
        } }, props.children));
};
export default LoginProvider;
